package service

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"micro-chat/internal/model"
	"micro-chat/internal/request"
	"micro-chat/internal/response"
	"net/http"
	"strconv"
)

func AddListBlack(c *gin.Context) {
	s := c.GetString("user_id")
	id, _ := strconv.Atoi(s)

	var req request.AddListBlackForm

	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "验证失败",
		})
		return
	}

	var black model.Blacklist
	black = model.Blacklist{
		UserId:      int32(id),
		BlackUserId: req.BlackUserId,
	}

	err := black.AddList()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "拉黑失败",
			"data":    err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "拉黑成功",
		"data":    black.Id,
	})
}

func FindBlackList(c *gin.Context) {
	value, _ := c.Get("user_id")
	id, _ := strconv.Atoi(fmt.Sprintf("%s", value))

	var user model.User
	var black model.Blacklist
	var blacklist []*response.FindBlackListFriend

	list, err := black.FindBlackList(int32(id))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    100000,
			"message": "黑名单加载失败",
			"data":    err.Error(),
		})
		return
	}

	for _, k := range list {
		user.FindUser(uint(k.BlackUserId))
		key := &response.FindBlackListFriend{
			Nickname:  user.Nickname,
			Avatar:    user.Avatar,
			Signature: user.Signature,
			CreatedAt: k.CreatedAt,
		}

		blacklist = append(blacklist, key)
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "黑名单加载成功",
		"data":    blacklist,
	})
}
